//
//  DPreferenceViewController.swift
//  Drop
//
//  Created by hushaohua on 2018/2/7.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DPreferencesCell : UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DPreferenceViewController: DMoreListViewController {

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil);
        self.title = "Preference";
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    override func registerTableViewCell() -> Void{
        self.tableView.register(DPreferencesCell.self, forCellReuseIdentifier: "Cell");
    }
    
    override func tableRowSections() -> [[DTableRowItem]] {
        var sections:[[DTableRowItem]] = [[DTableRowItem]]();
        var section0:[DTableRowItem] = [DTableRowItem]();
        section0.append(DTableRowItem(title:"免打扰", closure: { (indexPath) in
            self.startCustomUndisturbTime();
        }));
        sections.append(section0);
        
        var section1:[DTableRowItem] = [DTableRowItem]();
        section1.append(DTableRowItem(title:"提醒间隔", closure: { (indexPath) in
            self.changeNotificationInterval();
        }));
        sections.append(section1);
        
        var section3:[DTableRowItem] = [DTableRowItem]();
        section3.append(DTableRowItem(title:"Debug", closure: { (indexPath) in
            self.gotoDebug();
        }));
        sections.append(section3);
        
        return sections;
    }
    
    @objc func gotoDebug() -> Void{
        //DDebugDropTableViewController
        let controller = DDebugDropTableViewController();
        controller.hidesBottomBarWhenPushed = true;
        self.navigationController?.pushViewController(controller, animated: true);
    }
    
    @objc func changeNotificationInterval() -> Void{
        let controller = DIntervalViewController();
        controller.selectInterval = {() in
            self.tableView.reloadData();
            self.navigationController?.popViewController(animated: true);
        };
        controller.hidesBottomBarWhenPushed = true;
        self.navigationController?.pushViewController(controller, animated: true);
    }
    @objc func startCustomUndisturbTime() -> Void{
        DDroppingManager.sharedManager().removeAllNotifications();
    }
    
    lazy var undisturbSwitch:UISwitch = {
        var view:UISwitch = UISwitch();
        view.addTarget(self, action: #selector(undisturbSwitch(_:)), for: .valueChanged);
        return view;
    }();
    
    @objc func undisturbSwitch(_ sender:UISwitch) -> Void{
        DPreferences.defaultPreferences().doNotDisturb = sender.isOn;
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:DPreferencesCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DPreferencesCell;
        let row:DTableRowItem = self.sectionItems[indexPath.section][indexPath.row];
        cell.textLabel?.text = row.title;
        if (indexPath.section == 0){
            self.undisturbSwitch.isOn = DPreferences.defaultPreferences().doNotDisturb;
            cell.accessoryView = self.undisturbSwitch;
            cell.accessoryType = .none;
        }else{
            cell.detailTextLabel?.text = "\(DPreferences.defaultPreferences().notificationInterval / 60)";
            cell.accessoryType = .disclosureIndicator;
            cell.accessoryView = nil;
        }
        return cell;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
